package com.yituo.mjworld.core.web.core;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 过滤器
 * <p>
 * 用于实现自定义HttpRequest,HttpResponse包装类
 *
 * @author Administrator
 */
public class MyFilter implements Filter {
  /**
   * 本次开机服务器配置
   */
  private static long access_count = 0;

  /**
   * 过滤器销毁
   */
  @Override
  public void destroy() {
    System.out.println("本次开机共接收:" + access_count + " 个请求.");
  }

  /**
   * 过滤请求
   *
   * @param req
   * @param resp
   * @param chain
   * @throws ServletException
   * @throws IOException
   */
  @Override
  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
    access_count++;
    // 对进入的请求进行转换.
    MyHttpRequest myReq = new MyHttpRequest((HttpServletRequest) req);
    MyHttpResponse myRes = new MyHttpResponse((HttpServletResponse) resp);
    chain.doFilter(myReq, myRes);
  }

  /**
   * 实现初始化方法
   *
   * @param config
   * @throws ServletException
   */
  @Override
  public void init(FilterConfig config) throws ServletException {
    System.out.println("正在初始化自定义过滤器!");
  }
}
